package com.otherTest.java8;

import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @Description: 时间日期处理工具
 * @author: conceit
 * @Date: 2018/10/11 0011 14:49
 */
public class DateTimeUtil {

    public enum FormatEnum {

        yyyy年MM月dd日("yyyy年MM月dd日"),
        yyyy横杠MM("yyyy-MM"),
        yyyyMMdd("yyyyMMdd"),
        yyyyMM("yyyyMM");
        public String value;

        public String getValue() {
            return value;
        }

        FormatEnum(String value) {
            this.value = value;
        }
    }


    /**
     * @description: convertDateToLocalDateTime Date转换为LocalDateTime
     * @params [date]
     * @author: conceit
     * @return: java.time.LocalDateTime
     * @Date: 2018/10/11 0011 18:21
     */
    public static LocalDateTime convertDateToLocalDateTime(Date date) {
        return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
    }

    /**
     * @Description: convertLocalDateTimeToDate LocalDateTime转换为Date
     * @params [localDateTime]
     * @author: conceit
     * @return: java.util.Date
     * @Date: 2018/10/11 18:21
     */
    public static Date convertLocalDateTimeToDate(LocalDateTime localDateTime) {
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }

    /**
     * @Description: getCurrentDayStartTime 获取参数天数的零点时间
     * @params [localDateTime]
     * @author: conceit
     * @return: java.time.LocalDateTime
     * @Date: 2018/10/11 18:22
     */
    public static LocalDateTime getDayStartTime(LocalDateTime localDateTime) {
        return localDateTime.withHour(0).withMinute(0).withSecond(0).withNano(0);
    }

    public static Date getDayStartTime(Date date) {
        return convertLocalDateTimeToDate(getDayStartTime(convertDateToLocalDateTime(date)));
    }

    public static LocalDateTime getCurrentDayStartTime() {
        return getDayStartTime(LocalDateTime.now());
    }

    public static Date getCurrentDayStartTimes() {
        return convertLocalDateTimeToDate(getDayStartTime(LocalDateTime.now()));
    }


    /**
     * @Description: getCurrentDayEndTime 获取参数天数的最后时间
     * @params [localDateTime]
     * @author: conceit
     * @return: java.time.LocalDateTime
     * @Date: 2018/10/11 18:22
     */
    public static LocalDateTime getDayEndTime(LocalDateTime localDateTime) {
        return localDateTime.withHour(23).withMinute(59).withSecond(59).withNano(999999999);
    }

    public static Date getDayEndTime(Date date) {
        return convertLocalDateTimeToDate(getDayEndTime(convertDateToLocalDateTime(date)));
    }

    public static LocalDateTime getCurrentDayEndTime() {
        return getDayEndTime(LocalDateTime.now());
    }

    public static Date getCurrentDayEndTimes() {
        return convertLocalDateTimeToDate(getDayEndTime(LocalDateTime.now()));
    }

    /**
     * @Description: isSameDay 两个形参是否是同一天
     * @params [localDateTime1, localDateTime2]
     * @author: conceit
     * @return: boolean
     * @Date: 2018/10/11 18:23
     */
    public static boolean isSameDay(LocalDateTime localDateTime1, LocalDateTime localDateTime2) {
        if (localDateTime1.toLocalDate().toEpochDay() - localDateTime2.toLocalDate().toEpochDay() == 0) {
            return true;
        }
        return false;
    }

    /**
     * @Description: isSameDay 两个形参是否是同一天
     * @params [localDateTime1, localDateTime2]
     * @author: conceit
     * @return: boolean
     * @Date: 2018/10/11 0011 18:23
     */
    public static boolean isSameDay(Date date1, Date date2) {

        if (convertDateToLocalDateTime(date1).toLocalDate().toEpochDay() - convertDateToLocalDateTime(date2).toLocalDate().toEpochDay() == 0) {
            return true;
        }
        return false;
    }

    /**
     * @Description: getApartDay 两个形参相差的天数(自然天)。var2-var1
     * @params [localDateTime1, localDateTime2]
     * @author: conceit
     * @return: long
     * @Date: 2018/10/11 0011 18:24
     */
    public static long getApartDay(LocalDateTime localDateTime1, LocalDateTime localDateTime2) {
        return (localDateTime2.toLocalDate().toEpochDay() - localDateTime1.toLocalDate().toEpochDay());
    }

    /**
     * @Description: getApartDay 两个形参相差的天数(自然天)。var2-var1
     * @params [localDateTime1, localDateTime2]
     * @author: conceit
     * @return: long
     * @Date: 2018/10/11 0011 18:24
     */
    public static long getApartDay(Date date1, Date date2) {
        return convertDateToLocalDateTime(date2).toLocalDate().toEpochDay() - convertDateToLocalDateTime(date1).toLocalDate().toEpochDay();
    }

    /**
     * @Description: format 将date按照指定格式进行格式化
     * @params [Date, format]
     * @author: conceit
     * @return: java.lang.String
     * @Date: 2018/10/12 0012 18:05
     */
    public static String format(Date Date, String format) {
        return convertDateToLocalDateTime(Date).format(DateTimeFormatter.ofPattern(format));
    }

    /**
     * @Description: format 将localDateTime按照指定格式进行格式化
     * @params [localDateTime, format]
     * @author: conceit
     * @return: java.lang.String
     * @Date: 2018/10/12 0012 18:05
     */
    public static String format(LocalDateTime localDateTime, String format) {
        return localDateTime.format(DateTimeFormatter.ofPattern(format));
    }

    /**
     * @Description: parseLocalDateTime 将已经格式化的时间字符串，按照指定的格式，进行解析，返回LocalDateTime
     * @params [timeText, format]
     * @author: conceit
     * @return: java.time.LocalDateTime
     * @Date: 2018/10/12 0012 18:06
     */
    public static LocalDateTime parseLocalDateTime(String timeText, String format) {
        return LocalDateTime.parse(timeText, DateTimeFormatter.ofPattern(format));
    }

    /**
     * @Description: parseDate arseLocalDateTime 将已经格式化的时间字符串，按照指定的格式，进行解析，返回Date
     * @params [timeText, format]
     * @author: conceit
     * @return: java.util.Date
     * @Date: 2018/10/12 0012 18:07
     */
    public static Date parseDate(String timeText, String format) {
        return convertLocalDateTimeToDate(LocalDateTime.parse(timeText, DateTimeFormatter.ofPattern(format)));
    }


    /**
     * 某日期加上几天
     * @param date
     * @param day
     * @return
     * @throws ParseException
     */
    public static Date addDate(Date date,long day) throws ParseException {
        long time = date.getTime(); // 得到指定日期的毫秒数
        day = day*24*60*60*1000; // 要加上的天数转换成毫秒数
        time+=day; // 相加得到新的毫秒数
        return new Date(time); // 将毫秒数转换成日期
    }


    /**
     * 计算两个时间差
     */
    public static String getDatePoor(Date endDate, Date nowDate){
        long nd = 1000 * 24 * 60 * 60;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少天
        long day = diff / nd;
        return day + "天";
    }

    public static LocalDateTime addNano(LocalDateTime localDateTime, long nanos) {
        return localDateTime.plusNanos(nanos);
    }

    public static LocalDateTime addSecond(LocalDateTime localDateTime, long seconds) {
        return localDateTime.plusSeconds(seconds);
    }

    public static LocalDateTime addMinute(LocalDateTime localDateTime, long minutes) {
        return localDateTime.plusMinutes(minutes);
    }

    public static LocalDateTime addHour(LocalDateTime localDateTime, long hours) {
        return localDateTime.plusHours(hours);
    }

    public static LocalDateTime addDay(LocalDateTime localDateTime, long days) {
        return localDateTime.plusDays(days);
    }

    public static LocalDateTime addWeek(LocalDateTime localDateTime, long weeks) {
        return localDateTime.plusWeeks(weeks);
    }

    public static LocalDateTime addMonth(LocalDateTime localDateTime, long months) {
        return localDateTime.plusMonths(months);
    }

    public static LocalDateTime addYear(LocalDateTime localDateTime, long years) {
        return localDateTime.plusYears(years);
    }

    public static LocalDateTime minusNano(LocalDateTime localDateTime, long nanos) {
        return localDateTime.minusNanos(nanos);
    }

    public static LocalDateTime minusSecond(LocalDateTime localDateTime, long seconds) {
        return localDateTime.minusSeconds(seconds);
    }

    public static LocalDateTime minusMinute(LocalDateTime localDateTime, long minutes) {
        return localDateTime.minusMinutes(minutes);
    }

    public static LocalDateTime minusHour(LocalDateTime localDateTime, long hours) {
        return localDateTime.minusHours(hours);
    }

    public static LocalDateTime minusDay(LocalDateTime localDateTime, long days) {
        return localDateTime.minusDays(days);
    }

    public static LocalDateTime minusWeek(LocalDateTime localDateTime, long weeks) {
        return localDateTime.minusWeeks(weeks);
    }

    public static LocalDateTime minusMonth(LocalDateTime localDateTime, long months) {
        return localDateTime.minusMonths(months);
    }

    public static LocalDateTime minusYear(LocalDateTime localDateTime, long years) {
        return localDateTime.minusYears(years);
    }


    public static Date addNano(Date date, long nanos) {
        return convertLocalDateTimeToDate(convertDateToLocalDateTime(date).plusNanos(nanos));
    }

    public static Date addSecond(Date date, long seconds) {
        return convertLocalDateTimeToDate(convertDateToLocalDateTime(date).plusSeconds(seconds));
    }

    public static Date addMinute(Date date, long minutes) {
        return convertLocalDateTimeToDate(convertDateToLocalDateTime(date).plusMinutes(minutes));
    }

    public static Date addHour(Date date, long hours) {
        return convertLocalDateTimeToDate(convertDateToLocalDateTime(date).plusHours(hours));
    }

    public static Date addDay(Date date, long days) {
        return convertLocalDateTimeToDate(convertDateToLocalDateTime(date).plusDays(days));
    }

    public static Date addWeek(Date date, long weeks) {
        return convertLocalDateTimeToDate(convertDateToLocalDateTime(date).plusWeeks(weeks));
    }

    public static Date addMonth(Date date, long months) {
        return convertLocalDateTimeToDate(convertDateToLocalDateTime(date).plusMonths(months));
    }

    public static Date addYear(Date date, long years) {
        return convertLocalDateTimeToDate(convertDateToLocalDateTime(date).plusYears(years));
    }

    public static Date minusNano(Date date, long nanos) {
        return convertLocalDateTimeToDate(convertDateToLocalDateTime(date).minusNanos(nanos));
    }

    public static Date minusSecond(Date date, long seconds) {
        return convertLocalDateTimeToDate(convertDateToLocalDateTime(date).minusSeconds(seconds));
    }

    public static Date minusMinute(Date date, long minutes) {
        return convertLocalDateTimeToDate(convertDateToLocalDateTime(date).minusMinutes(minutes));
    }

    public static Date minusHour(Date date, long hours) {
        return convertLocalDateTimeToDate(convertDateToLocalDateTime(date).minusHours(hours));
    }

    public static Date minusDay(Date date, long days) {
        return convertLocalDateTimeToDate(convertDateToLocalDateTime(date).minusDays(days));
    }

    public static Date minusWeek(Date date, long weeks) {
        return convertLocalDateTimeToDate(convertDateToLocalDateTime(date).minusWeeks(weeks));
    }

    public static Date minusMonth(Date date, long months) {
        return convertLocalDateTimeToDate(convertDateToLocalDateTime(date).minusMonths(months));
    }

	/**
	 * 指定时间减去指定年数
	 * @param date
	 * @param years
	 * @return
	 */
	public static Date minusYear(Date date, long years) {
        return convertLocalDateTimeToDate(convertDateToLocalDateTime(date).minusYears(years));
    }

    public static Integer getCurrentTimeSec() {
        return Long.valueOf(System.currentTimeMillis() / 1000).intValue();
    }


    /**
     * 获取指定时间是月的第几天
     * @param date
     * @return
     */
    public static int getDayOfMonth(Date date){
        return convertDateToLocalDateTime(date).getDayOfMonth();
    }


    /**
     * 获取指定时间的总天数
     * @param date
     * @return
     */
    public static int countDayInMonth(Date date){
        return convertDateToLocalDateTime(date).getMonth().maxLength();
    }

	/**
	 * 获取指定时间 月的第一天
	 * @param date
	 * @return
	 */
	public static Date getFirstDayOfMonth(Date date){
		return convertLocalDateTimeToDate(convertDateToLocalDateTime(date).withDayOfMonth(0));
	}

	/**
	 * 获取指定时间的 年份
	 * @param date
	 * @return
	 */
	public static int getYear(Date date){
		return convertDateToLocalDateTime(date).getYear();
	}

	/**
	 * 获取指定时间的 月
	 * @param date
	 * @return
	 */
	public static int getMonth(Date date){
		return convertDateToLocalDateTime(date).getMonth().getValue();
	}

    /**
     * 将长时间格式字符串转换为时间 yyyy-MM-dd HH:mm:ss
     *
     * @param strDate
     * @return
     */
    public static Date strToDateLong(String strDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = formatter.parse(strDate, pos);
        return strtodate;
    }
    /**
     * 将长时间格式时间转换为字符串 yyyy-MM-dd HH:mm:ss
     *
     * @param dateDate
     * @return
     */
    public static String dateToStrLong(Date dateDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(dateDate);
        return dateString;
    }





}
